package com.eric.windows.imgDispose;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;

import com.eric.windows.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Touch;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_surface_test)
public class SurfaceTestActivity extends Activity {
    @ViewById
    SurfaceView sv_surfaces;
    private Paint paint;
    private SurfaceHolder holder;

    @AfterViews
    void initView(){
        paint = new Paint();
        holder = sv_surfaces.getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {
            /**
             * SurfaceView 创建时调用该方法
             * @param holder
             */
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                Toast.makeText(SurfaceTestActivity.this, "SurfaceView创建", Toast.LENGTH_SHORT).show();
            }

            /**
             * SurfaceView 的格式或者大小改变时回调该方法
             * @param holder
             * @param format
             * @param width
             * @param height
             */
            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                Toast.makeText(SurfaceTestActivity.this, "SurfaceView改变了", Toast.LENGTH_SHORT).show();
                //锁定整个surfaceView
                Canvas  canvas  = holder.lockCanvas();
                Bitmap bitmap  = BitmapFactory.decodeResource(getResources(),R.mipmap.pic_2);
                //绘制背景色
//                canvas.drawBitmap(bitmap, 0, 0, null);
                canvas.drawColor(Color.BLUE);
                //绘制完成 释放画布，提交修改
                holder.unlockCanvasAndPost(canvas);
                //再次锁定surface "持久化上次所绘制的内容"
                holder.lockCanvas(new Rect(0,0,0,0));
                holder.unlockCanvasAndPost(canvas);
            }

            /**
             * SurfaceView 销毁时回调该方法
             * @param holder
             */
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                Toast.makeText(SurfaceTestActivity.this, "SurfaceView销毁", Toast.LENGTH_SHORT).show();

            }

        });

    }

    @Touch(R.id.sv_surfaces)
    boolean onTouch(View view, MotionEvent event){
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            int cx = (int) event.getX();
            int cy = (int) event.getY();
            Canvas  canvas = holder.lockCanvas(new Rect(cx - 50 ,cy - 50 , cx + 50 ,cy + 50));
            canvas.save();
            paint.setColor(Color.RED);
            canvas.rotate(30,cx,cy);
            canvas.drawRect(cx - 40 ,cy - 40,cx,cy,paint);
            canvas.restore();
            paint.setColor(Color.GREEN);
            canvas.drawRect(cx ,cy,cx + 40 ,cy + 40,paint);
            holder.unlockCanvasAndPost(canvas);
        }
        return false;
    }


}
